package com.shawn.example.mvvmdemo.ui

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.viewModels
import com.shawn.example.mvvmdemo.databinding.ActivitySecondBinding
import com.jetpack.mvvm.lib.base.extention.appViewModules
import com.shawn.example.mvvmdemo.viewmodel.RepViewModel
import com.shawn.example.mvvmdemo.viewmodel.SecondViewModel

class SecondActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "SecondActivity"
        fun start(context: Context) {
            context.startActivity(Intent(context, SecondActivity::class.java))
        }
    }

    private val viewModel: RepViewModel by appViewModules()
    private val secViewModel by viewModels<SecondViewModel>()
    private val binding: ActivitySecondBinding by lazy {
        ActivitySecondBinding.inflate(LayoutInflater.from(this))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        viewModel.getRepList()
        binding.tvUpdateValue.setOnClickListener {
            viewModel.repListLiveData.value = "second ${System.currentTimeMillis()}"
        }

        secViewModel.testFlow()
        observerData()
    }

    private fun observerData() {
        secViewModel.uiLiveData.observe(this) {
            binding.tvUpdateValue setText "$it"
        }
    }

    private infix fun TextView.setText(txt: String?) {
        this.text = txt
    }
}